// pages/map/map.js
Page({

  data: {
    longitude: '115.662460',
    latitude: '37.743920',
    address: '',
    longitude_1: '',
    latitude_1: '',
    markers: [
      {
        id: 0,
        longitude: 115.662460,
        latitude: 37.743920,
        iconPath: '../../images/map.png',
        title: '衡水学院广场'
      },
      {
        id: 1,
        longitude: 115.661406,
        latitude: 37.742915,
        iconPath: '../../images/map.png',
        title: '衡水学院物理系'
      }
    ]
  },
  onShow: function() {
    this.getMyLocation()
  },

  onLoad: function (options) {
    
  },
  getMyLocation: function() {
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude
        })
      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          content: '如果拒绝使用你的地理位置信息，你将不能获得相关服务',
          showCancel: false,
          success: function(res) {
            wx.openSetting()
          }
        })
        
      }
    })
  },
  chooseMyAddress: function() {
    var that = this
    wx.chooseLocation({
      success: function(res) {
        console.log(res)
        that.setData({
          address: res.address + '(' + res.name + ')',
          longitude_1: res.longitude,
          latitude_1: res.latitude
        })
      },
      fail: function(res) {
        console.log(res)
      }
    })
  },
  openMap: function() {
    wx.openLocation({
      latitude: this.data.latitude_1,
      longitude: this.data.longitude_1,
      success: function(res) {
        console.log(res)
      },
      fail: function(res) {
        console.log(res)
      },
    })
  },
  handleMarker: function(e) {
    console.log(e)
    var markerId = e.detail.markerId
    var marker = this.data.markers[markerId]
    this.setData({
      address: marker.title,
      longitude_1: marker.longitude,
      latitude_1: marker.latitude
    })
  }
})